import java.util.HashSet;
import java.util.Set;

public class Solution792 {
    public int numMatchingSubseq(String s, String[] words) {
        int[] point=new int[words.length];
        Set<Integer>[] sets=new Set[26];
        for (int i=0;i<26;i++){
            sets[i]=new HashSet<>();
        }
        for (int i=0;i<words.length;i++){
            sets[words[i].charAt(0)-'a'].add(i);
        }
        for (char c : s.toCharArray()) {
            Set<Integer> set=new HashSet<>(sets[c-'a']);
            sets[c-'a'].clear();
            for (Integer integer : set) {
                point[integer]++;
                if (point[integer]<words[integer].length()){
                    sets[words[integer].charAt(point[integer])-'a'].add(integer);
                }
            }
        }
        int ans=0;
        for (int i=0;i<words.length;i++){
            if (point[i]==words[i].length()){
                ans++;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution792().numMatchingSubseq("abcde",new String[]{"a","bb","acd","ace"}));
    }
}
